package com.heima.wemedia.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.JsonNull;
import com.heima.model.common.constants.WmNewsMessageConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.utils.threadlocal.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.service.WmNewsAuditService;
import com.heima.wemedia.service.WmNewsMaterialService;
import com.heima.wemedia.service.WmNewsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class WmNewsServiceImpl extends ServiceImpl<WmNewsMapper, WmNews> implements WmNewsService {

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 查询文章列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmNewsPageReqDto dto) {
        //1.非空判断
        dto.checkParam();
        //2.分页查询
        IPage<WmNews> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmNews> queryWrapper= Wrappers.lambdaQuery();
        //2.1 根据状态查询
        if(dto.getStatus()!=null){
            queryWrapper.eq(WmNews::getStatus,dto.getStatus());
        }

        //2.2 根据关键字查询
        if(!StringUtils.isEmpty(dto.getKeyword())){
            queryWrapper.like(WmNews::getTitle,dto.getKeyword());
        }

        //2.3 根据频道id查询
        if(dto.getChannelId()!=null){
            queryWrapper.eq(WmNews::getChannelId,dto.getChannelId());
        }

        //2.4 根据开始和结束时间查询
        if(dto.getBeginPubdate()!=null && dto.getEndPubdate()!=null){
            queryWrapper.between(WmNews::getPublishTime,dto.getBeginPubdate(),dto.getEndPubdate());
        }

        //2.5 根据登录用户id查询
        queryWrapper.eq(WmNews::getUserId, WmThreadLocalUtil.getUser().getId());
        queryWrapper.orderByDesc(WmNews::getCreatedTime);

        page=super.page(page,queryWrapper);

        //3.返回数据结果
        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 保存-修改-提交草稿为一体的方法
     *  主方法
     * @param dto
     * @return
     */
    @Autowired
    private WmNewsMaterialService wmNewsMaterialService;
    @Autowired
    private WmMaterialService wmMaterialService;
    @Autowired
    private WmNewsAuditService wmNewsAuditService;
    @Transactional
    @Override
    public ResponseResult submit(WmNewsDto dto) {
        Short isSubmit = dto.getStatus();
//1.判断必要参数不能为空
        if(StringUtils.isBlank(dto.getTitle()) || StringUtils.isBlank(dto.getContent())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.创建或修改文章
//2.1 复制dto给pojo
        WmNews wmNews = new WmNews();
        BeanUtils.copyProperties(dto,wmNews);
//2.2 单独处理images的值（将dto里的封面图片列表转为逗号拼接的地址字符串）
        if(dto.getImages()!=null && dto.getImages().size()>0){
//String coverImageStr = String.join(",", dto.getImages());
// String coverImageStr = StringUtils.join(dto.getImages(), ",");
            String coverImageStr = dto.getImages().stream().collect(Collectors.joining(","));
            wmNews.setImages(coverImageStr);
        }
//2.3 当布局方式为自动布局时，则需要将布局方式设置为临时为空（后续逻辑还会继续决定布局方式和更新布局方式）
        if(dto.getType()==-1){
            wmNews.setType(null);
        }
//2.4 根据id参数值决定创建文章还是修改文章
        ResponseResult responseResult = this.saveOrUpdateWmNews(dto, isSubmit, wmNews);
        if(responseResult!=null){
            return responseResult;
        }
//必须是提交审核状态，才能执行后续更多逻辑
        if(isSubmit.equals(WmNews.Status.SUBMIT.getCode())){
//3.从文章内容中抽取全部图片地址列表
            List<String> contentImageUrlList = extractContentImageUrlList(dto);
            //4.保存内容图片与文章关系
            if(contentImageUrlList.size()>0){
                responseResult = this.saveRelation(contentImageUrlList, wmNews.getId(), (short)0);
                if(responseResult!=null){
                    return responseResult;
                }
            }
//5.保存封面图片与文章关系
            responseResult = this.saveRelationForCoverImage(dto, wmNews, contentImageUrlList);
            if (responseResult!= null){
                return responseResult;
            }
            //5. 调用文章自动审核逻辑 (线程池异步处理)
            wmNewsAuditService.auditWmNews(wmNews, contentImageUrlList);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    /**
     * 保存封面图片与文章关系
     * @param dto
     * @param wmNews
     * @param contentImageUrlList
     * @return
     */
    private ResponseResult saveRelationForCoverImage(WmNewsDto dto, WmNews wmNews, List<String> contentImageUrlList) {
        ResponseResult responseResult;
        //1 获取dto封面图片地址列表
        List<String> coverImageUlrList = dto.getImages();
//2 如果是自动布局，则根据自动匹配规则决定最终封面布局方式及更新文章布局方式和封面图片
        if(dto.getType()==-1){
            int size = contentImageUrlList.size();
//2.1 根据自动匹配规则决定封面图片和布局方式
//自动匹配规则1：如果内容图片地址数量大于等于3，则取前3张图片当做封面图片，布局决定为多图布局
            if(size>=3){
//coverImageUlrList = contentImageUrlList.subList(0,3);
                coverImageUlrList = contentImageUrlList.stream().limit(3).collect(Collectors.toList());
                wmNews.setType((short)3);//多图布局
            } else if(size>=1 && size<3){
//自动匹配规则2：如果内容图片地址数量大于等于1，则取前1张图片当做封面图片，布局决定为单图布局
                coverImageUlrList = contentImageUrlList.stream().limit(1).collect(Collectors.toList());
                wmNews.setType((short)1);//单图布局
            } else {
//自动匹配规则3：如果内容图片地址数量大于等于0，布局决定为无图布局
                coverImageUlrList = new ArrayList<>();
                wmNews.setType((short)0);//无图布局
            }
//2.2 如果匹配到了封面图片，则将封面图片地址转为逗号拼接的字符串
            if(coverImageUlrList.size()>0){
                String coverImageStr = coverImageUlrList.stream().collect(Collectors.joining(","));
                wmNews.setImages(coverImageStr);
            }
//2.3 更新文章的布局方式和封面图片
            this.updateById(wmNews);
        }
//3 保存封面图片与文章的关系到关系表
        if(coverImageUlrList!=null && coverImageUlrList.size()>0){
            responseResult = this.saveRelation(coverImageUlrList, wmNews.getId(), (short)1);
            if(responseResult!=null){
                return responseResult;
            }
        }
        return null;
    }
    /**
     * 从文章内容中抽取全部图片地址列表
     * @param dto
     * @return
     */
    private static List<String> extractContentImageUrlList(WmNewsDto dto) {
        List<String> contentImageUrlList = new ArrayList<>();
        List<Map> mapList = JSON.parseArray(dto.getContent(), Map.class);
        if(mapList!=null && mapList.size()>0){
            for (Map<String,String> map : mapList) {
                String type = map.get("type");
                if("image".equals(type)){
                    String contentImageUrl = map.get("value");//内容中的图片地址
                    contentImageUrlList.add(contentImageUrl);
                }
            }
        }
        return contentImageUrlList;
    }
    private ResponseResult saveOrUpdateWmNews(WmNewsDto dto, Short isSubmit, WmNews wmNews) {
//补充设置更多属性的值
        wmNews.setUserId(WmThreadLocalUtil.getUser().getId());//自媒体用户id
        wmNews.setEnable((short)1);//默认值：已上架
        wmNews.setStatus(isSubmit);//0-保存草稿 1-提交审核
        wmNews.setSubmitedTime(new Date());//提交时间
//ID无值，就创建文章
        if(wmNews.getId()==null){
            wmNews.setCreatedTime(new Date());//创建时间
            this.save(wmNews);
        } else {
//ID有值，就修改文章且删除关系
//判断文章是否存在
            Integer count = this.lambdaQuery().eq(WmNews::getId, dto.getId()).count();
            if(count==0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "文章不存在无法修改");
            }
//修改文章
            this.updateById(wmNews);
//删除关系 delete from wm_news_material where news_id=?
            wmNewsMaterialService.remove(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getNewsId, wmNews.getId()));
        }
        return null;
    }
    /**
     * 公共方法：保存素材与文章的对应关系到关系表中
     * @param imageUrlList 素材列表
     * @param wmNewsId 自媒体文章id
     * @param type 引用类型0-内容引用 1-主图引用
     * @return
     */
    private ResponseResult saveRelation(List<String> imageUrlList,Integer wmNewsId,Short type){
//1 根据内容中的素材地址从素材表中查询素材列表 select id from wm_material where url in (?,?,?...)
        List<WmMaterial> wmMaterialList = wmMaterialService.lambdaQuery().in(WmMaterial::getUrl, imageUrlList)
                .select(WmMaterial::getId).list();
//2 判断是否所有素材地址都查询到了素材
        if(wmMaterialList.size()!=imageUrlList.size()){
            return ResponseResult.errorResult(AppHttpCodeEnum.MATERIASL_REFERENCE_FAIL);
        }
        //3 获取素材列表中的所有素材ID列表
        List<Integer> materialIdList = wmMaterialList.stream().map(WmMaterial::getId).collect(Collectors.toList());
//4 遍历素材ID列表逐一构建关系数据
        Short ord = 0;
        List<WmNewsMaterial> wmNewsMaterialList = new ArrayList<>();
        for (Integer materialId : materialIdList) {
            WmNewsMaterial wmNewsMaterial = new WmNewsMaterial();
            wmNewsMaterial.setNewsId(wmNewsId); //文章id
            wmNewsMaterial.setMaterialId(materialId); //素材id
            wmNewsMaterial.setType(type);//引用类型：0-内容引用 1-主图引用
            wmNewsMaterial.setOrd(ord++);//序号
            wmNewsMaterialList.add(wmNewsMaterial);
        }
//5 批量保存关系数据 insert into wm_news_material (news_id,material_id,type,ord) values (1,101,0,1),(1,102,0,2),(1,103,0,3)
        wmNewsMaterialService.saveBatch(wmNewsMaterialList);
        return null;
    }

    /**
     * 文章上下架
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult downOrUp(WmNewsDto dto) {

        // 1、校验文章id是否存在
        if (dto.getId() == null ){
            throw new RuntimeException("文章id不存在");
        }

        // 2、根据文章id获取数据库中文章的信息
        WmNews wmNews = this.getById(dto.getId());
        if (wmNews == null){
            throw new RuntimeException("文章不存在");
        }

        // 3、获取当前文章的状态是否为已上线状态
        if (wmNews.getStatus() != 9){
            throw new RuntimeException("文章状态不是发布状态，不能上下架");
        }

        // 4、获取当前文章的上下架装填并进行修改
        if (dto.getEnable() == 0){
            wmNews.setEnable(dto.getEnable());
            boolean result = this.updateById(wmNews);
            if (!result){
                throw new RuntimeException("文章上下架失败");
            }
        }else {
            wmNews.setEnable(dto.getEnable());
            boolean result = this.updateById(wmNews);
            if (!result){
                throw new RuntimeException("文章上下架失败");
            }
        }

        // 5、发送消息
        Map<String,Object> map = new HashMap<>();
        map.put("articleId",wmNews.getArticleId());
        map.put("enable",wmNews.getEnable());
        rabbitTemplate.convertAndSend( WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC, WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_ROUTINGKEY, JSONUtil.toJsonStr( map));

        return ResponseResult.okResult("文章上下架完成");
    }
}
